<template>
	<view>
		<view class="content">
			<view class="list">
				<view class="row" @click="navTo('addBillType')">
					<view class="title">抬头类型</view>
					<view class="right">
						<view class="tis">{{listTips}}</view>
						<view class="yticon icon-you"></view>
					</view>
				</view>
				<view class="row" >
					<view class="title">发票抬头</view>
					<view class="right">
						<input type="text"  placeholder="抬头名称" v-model="dataModal.vbrk"   class="tis"  style="text-align: right;"/>
					</view>
				</view>
				<view class="row" v-if="dataModal.vbrkType==2">
					<view class="title">纳税人识别号</view>
					<view class="right">
						<input type="text"  placeholder="纳税人识别号" v-model="dataModal.taxNumber"   class="tis"  style="text-align: right;"/>
					</view>
				</view>
			</view>
			<view class="list" v-if="dataModal.vbrkType==2">
				<view class="row" >
					<view class="title">开户银行</view>
					<view class="right">
						<input type="text" placeholder="选填" class="tis"  v-model="dataModal.bankName" style="text-align: right;"/>
					</view>
				</view>
				<view class="row" >
					<view class="title">银行账号</view>
					<view class="right">
						<input type="text" placeholder="选填"  class="tis" v-model="dataModal.bankAccount"  style="text-align: right;"/>
					</view>
				</view>
				<view class="row" >
					<view class="title">企业地址</view>
					<view class="right">
						<input type="text" placeholder="选填" class="tis" v-model="dataModal.companyAddress"  style="text-align: right;"/>
					</view>
				</view>
				<view class="row" >
					<view class="title">企业电话</view>
					<view class="right">
						<input type="text"  placeholder="选填" class="tis" v-model="dataModal.companyTel"  style="text-align: right;"/>
					</view>
				</view>
			</view>
			<view class="list">
				<view class="row" >
					<view class="title">设置为默认抬头</view>
					<view class="right">
						<switch :checked="dataModal.isDefault" color="#fa436a" @change="switchChange($event)" />
					</view>
				</view>
			</view>
		</view>
		<view class="foot">
			<view class="add-btn" @click="addData">
				保存
			</view>
		</view>
		<uni-popup ref="image" type="center" :custom="true">
			<view class="uni-image">
				<image class="image" :src="popImage" mode="" style="width: 80rpx;height: 80rpx;"/>
				<view class="font34 colorfont">
					{{showToastTips}}
				</view>
			</view>
		</uni-popup>
	</view>
	
</template>
<script>
	import uniPopup from '@/components/uni-popup/uni-popup.vue'
	export default {
		components: {
			uniPopup
		},
		data() {
			return {
				listTips:'请选择类型',
				manageType:'add',
				showToastTips:'',
				popImage:'',
				dataModal:{
					vbrkType:'',
					vbrk:'',
					taxNumber:'',
					bankName :'',
					bankAccount:'',
					companyAddress:'',
					companyTel :'',
					isDefault :false,
					type:2
				}
			};
		},
		computed: {
			
		},
		onLoad(option) {
			
			let title = '新增发票抬头';
			if (option.type === 'edit') {
				title = '编辑发票抬头'
				const optionData = JSON.parse(option.data)
				this.dataModal = optionData;
				this.listTips =`${optionData.vbrkType=='1' ? '个人/或事业单位': '企业'}` 
			}
			this.dataModal.type = option.id
			this.manageType = option.type;
			uni.setNavigationBarTitle({
				title
			})
		},

		methods: {
			switchChange(e){
				console.log(e)
				this.dataModal.isDefault = e.detail.value
			},
			navTo(url){
				this.$Router.push({name:url})
			},
			addData(){
				uni.showLoading({
				    title: '加载中'
				});
				let data = this.dataModal;
				if (!data.vbrkType) {
					this.popImage = '/static/useCenter/error.png';
					this.$refs['image'].open();
					this.showToastTips='请选择类型'
					return;
				}
				if (!data.vbrk) {
					this.popImage = '/static/useCenter/error.png';
					this.$refs['image'].open();
					this.showToastTips='请输入发票抬头'
					return;
				}
				if(this.dataModal.vbrkType==2){
					
				
				if (!data.taxNumber) {
					this.popImage = '/static/useCenter/error.png';
					this.$refs['image'].open();
					this.showToastTips='请填写纳税人识别号'
					return;
				}
				}
				var requestPost = '';
				if (this.manageType == 'edit') {
					// data['invoiceId '] = this.dataModal.invoiceId;
					requestPost='PUT'
				} else {
					requestPost='POST'
				}
				console.log(data)
				this.$http
					.request({
						method: requestPost, // 请求方法必须大写
						url: this.$requestApi.invoice,
						data: data
					})
					.then(result => {
						let _data = result.data;
						console.log(_data)
						if (_data.code === 200) {
							uni.hideLoading();
							// this.$api.prePage().refreshList();
							this.popImage = '/static/useCenter/login-success.png';
							this.$refs['image'].open();
							this.showToastTips=`发票抬头${this.manageType=='edit' ? '修改': '添加'}成功`;
							setTimeout(() => {
								uni.navigateBack()
							}, 800)
						}
					})
					.catch(err => {
						console.log(err);
					});
			},
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f3f3f3;
	}

	.yticon {
		font-size: 30upx;

	}

	.content {
		padding-bottom: 20upx;

		.list {
			width: 100%;
			padding-left: 4%;
			background-color: #fff;
			margin-bottom: 20upx;

			.row {
				widows: 100%;
				padding-right: 20upx;
				min-height: 90upx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				border-bottom: solid 1upx #eee;

				&:last-child {
					border-bottom: none;
				}

				.title {
					font-size: 32upx;
					color: #0B0E11;
				}

				.right {
					display: flex;
					align-items: center;
					color: #999;

					.tis {
						font-size: 26upx;
						margin-right: 5upx;
						max-height: 200upx;

						image {
							width: 155upx;
							height: 155upx;
							border-radius: 100%;
							margin: 10upx 0;
						}
					}

					.yticon {
						width: 40upx;
						color: #cecece;
					}
				}

			}
		}
	}
	.foot{
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		text-align: center;
		height: 100upx;
		line-height: 100upx;
		.add-btn{
			display: inline-block;
			width: 690upx;
			border-radius: 40upx;
			height: 70upx;
			text-align: center;
			line-height: 70upx;
			background:linear-gradient(90deg,rgba(255,94,0,1) 0%,rgba(255,35,0,1) 100%);
			font-size: 36upx;
			color: #fff;
		}
	}
</style>
